package com.bankcomm.luafuction;

import org.keplerproject.luajava.LuaException;
import org.keplerproject.luajava.LuaState;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Message;
import android.telephony.TelephonyManager;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.ui.BaseViewCopy;
import com.rytong.tools.datastorage.PackageManagerAndroid;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

public class LuaSystem {
    private String DEVICEID = "deviceID";
    private String NAME = "name";
    private String MODEL = "model";
    private String ENVIRONMENT = "environment";
    private String PLATFORM = "platform";
    private String VERSION = "version";
    
    private String CLIENT_VERSION = "client-version";
    private String CLIENT_VERSION_CODE = "client-version-code";
    private TelephonyManager tm = (TelephonyManager) ConfigManager.currentView_
            .getSystemService(ConfigManager.currentView_.TELEPHONY_SERVICE);

    public LuaSystem() {
        try {
            Component.LUASTATE.pushObjectValue(this);
            Component.LUASTATE.setGlobal("luasystem");
            final String luahead = "system = {}; ";
            final String luafc1 = "function system:getInfo(name) local a1 = luasystem:getInfo(name); return a1; end;";
            final String luafc2 = "function system:openURL(url) luasystem:openURL(url); end;";
            final String luafc3 ="function system:setPushMsg(flag) luasystem:setPushMsg(flag); end;";
            final String luafc4 ="function system:setDisableSnapShot(flag) luasystem:setDisableSnapShot(flag); end;";
            final String luafc5 = "function system:getShareDB(name) local a1 = luasystem:getShareDB(name); return a1; end;";
            Component.LUASTATE.gc(LuaState.LUA_GCCOLLECT, 0);
            Component.LUASTATE.LdoString(luahead.concat(luafc1).concat(luafc2).concat(luafc3).concat(luafc4).concat(luafc5));
        } catch (LuaException e) {
            // TODO Auto-generated catch block
            Utils.printException(e);
        }
    }

    /**
     * 在移动设备浏览器中打开指定的网址，或跳转到移动设备中的邮件系统，或调转到拨打电话功能
     * 
     * @param url
     *            the url to open<br>
     *            url类型分为以下三种情况：<br>
     *            1.Web Link: “http://www.rytong.com” <br>
     *            2.Phone Number: “tel:01058856690” <br>
     *            3.Email: “mailto:service@rytong.com” <br>
     */
    public void openURL(Object url) {
        String urlStr = String.valueOf(url);
        if (urlStr != null) {
            if (urlStr.startsWith("http://") || urlStr.startsWith("https://") || urlStr.startsWith("tel:") || urlStr.startsWith("mailto:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.parse(urlStr);
                intent.setData(uri);
                ConfigManager.currentView_.startActivity(intent);
            }
        }
    }

    public Object getInfo(Object name) {
        if (name instanceof String) {
            String infoName = (String) name;
            String infoValue = "";
            if (DEVICEID.equals(infoName)) {
                infoValue = getDeviceId();
            } else if (NAME.equals(infoName)) {
                infoValue = Utils.getName();
            } else if (MODEL.equals(infoName)) {
                infoValue = Utils.getModel();
            } else if (ENVIRONMENT.equals(infoName)) {
                infoValue = getEnvironment();
            } else if (PLATFORM.equals(infoName)) {
                infoValue = Utils.getPlayform();
            } else if (VERSION.equals(infoName)) {
                infoValue = Utils.getVersion();
            } else if (CLIENT_VERSION.equals(infoName)){
            	infoValue = getClientVersion(CLIENT_VERSION);
            } else if (CLIENT_VERSION_CODE.equals(infoName)){
            	infoValue = getClientVersion(CLIENT_VERSION_CODE);
            } else {
                return null;
            }
            return infoValue;

        } else {
            return null;
        }
    }

	public String getClientVersion(String name) {
		PackageInfo packageInfo = null;
		try {
			packageInfo = ConfigManager.currentView_.getPackageManager()
					.getPackageInfo(
							ConfigManager.currentView_.getPackageName(),
							PackageManager.GET_CONFIGURATIONS);
			if (CLIENT_VERSION.equals(name)){
            	return packageInfo.versionName;
            }else if(CLIENT_VERSION_CODE.equals(name)){            	
            	return String.valueOf(packageInfo.versionCode);
            }
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
    
    private String getEnvironment() {
        // 程序运行环境,模拟器还是真机
        String deviceID = tm.getDeviceId();
        boolean isEmulator = "000000000000000".equalsIgnoreCase(deviceID);
        if (isEmulator) {
            return "simulator";
        } else {
            return "device";
        }
    }

    private String getDeviceId() {
        // 获取imei号
        return tm.getDeviceId();
    }
    
    /**
     * 是否打开消息推送页面 //true打开/false关闭
     * @param flag
     */
    public void setPushMsg(String flag){
      PackageManagerAndroid.ANDROIDDB.save("pushMessage", flag);
    }
    
    public void setDisableSnapShot(String flag){
    	//yes开/no关闭
    	Message msg=new Message();
    	msg.what=1000;
    	msg.obj=flag;
        BaseViewCopy.getInstence().DisableSnapShotHandler.sendMessage(msg);
    }
    
	public Object getShareDB(Object name) {
		// pushMessage/snapshot
		String s = PackageManagerAndroid.ANDROIDDB.get(String.valueOf(name));
		if (name.equals("pushMessage")) {
	        if(null == s || "".equalsIgnoreCase(s) ||s.equals("false")){
	        	s = "";	
	        }else if(s.equals("true")){
	        	s="true";
	        }
		}
		if (name.toString().equals("snapshot")) {
			if (s.equals("")) {
				s = "no";
			}
		}
		return s;
	}
}
